# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         daima02
# Description:
# Author:       ASUS
# Date:         2019/4/29
#-------------------------------------------------------------------------------



import requests
#飘红不代表代码出错
from lxml import etree
import os

class xiaoshuo(object):
    #请求第一个页面，拿到小说名，小说详情链接，创建小说名的文件夹
    def firstFile(self):
        response = requests.get('https://www.qidian.com/all')
        html = etree.HTML(response.text)
        # xsName_list = html.xpath('/html/body/div[1]/div[5]/div[2]/div[2]/div/ul/li[1]')
        xsName_list = html.xpath('//div[@class="book-mid-info"]/h4/a/text()')
        # print(xsName_list)
        xsName_url = html.xpath('//div[@class="book-mid-info"]/h4/a/@href')
        for xsName,xsUrl in zip(xsName_list,xsName_url):
            # print(xsName,xsUrl)
            if os.path.exists(xsName) == False:
                os.mkdir(xsName)
            self.next_Files(xsName,xsUrl)

#请求第二个页面，拿到章节名，章节链接。
    def next_Files(self,xsName,xsUrl):
        litleurl = 'http:'+ xsUrl
        # print(litleurl)
        response2 = requests.get(litleurl)
        chapterName= etree.HTML(response2.text)
        # print(chapterName)
        chapterName_list = chapterName.xpath('//ul[@class="cf"]/li/a/text()')
        # print(chapterName_list)
        chapterSrc_list = chapterName.xpath('//ul[@class="cf"]/li/a/@href')
        for chapterName,chapterSrc in zip(chapterName_list,chapterSrc_list):
            # print(chapterName,chapterSrc)
            self.finally_Files(chapterName,chapterSrc,xsName)

#请求第三个页面，拿到小说正文数据，保存数据
    def finally_Files(self,chapterName,chapterSrc,xsName):
        zhengwenurl = 'http:'+ chapterSrc
        response3 = requests.get(zhengwenurl)
        zwNengrong = etree.HTML(response3.text)
        zhengwen = '\n'.join(zwNengrong.xpath('//div[@class="read-content j_readContent"]/p/text()'))
        filename = xsName + "//" + chapterName + '.txt'
        print('正在爬取：' +filename)
        with open(filename,'w',encoding='utf-8') as f:
            f.write(zhengwen)


if __name__ == '__main__':
    xshuo = xiaoshuo()
    xshuo.firstFile()


